﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public  class CardTypeDBAccess
    {

        /// <summary>
        /// Insert CardType
        /// </summary>
        /// <param name="objCardType"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>

        public bool AddNewCardType(CardType objCardType, String CONNECTION_STRING)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
               // new SqlParameter("@CardTypeID", objCardType.CardTypeID),
                new SqlParameter("@Created", objCardType.Created),
                new SqlParameter("@Updated", objCardType.Updated),
                new SqlParameter("@Isactive", objCardType.Isactive),
                new SqlParameter("@Name", objCardType.Name),
                new SqlParameter("@Descriptions", objCardType.Descriptions),
            };
            return SqlDBHelper.ExecuteNonQuery("AddNewCardType", CommandType.StoredProcedure, CONNECTION_STRING, parameter);
        }

        /// <summary>
        /// Update CardType
        /// </summary>
        /// <param name="objCardType"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public bool UpdateCardType(CardType objCardType, String CONNECTION_STRING)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@CardTypeID", objCardType.CardTypeID),
                new SqlParameter("@Created", objCardType.Created),
                new SqlParameter("@Updated",objCardType.Updated),
                new SqlParameter("@Isactive", objCardType.Isactive),
                new SqlParameter("@Name", objCardType.Name),
                new SqlParameter("@Descriptions", objCardType.Descriptions),
            };

            return SqlDBHelper.ExecuteNonQuery("UpdateCardType", CommandType.StoredProcedure, CONNECTION_STRING, parameters);
        }

        /// <summary>
        /// Delete CardType
        /// </summary>
        /// <param name="CardTypeID"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public bool DeleteCardType(int CardTypeID, String CONNECTION_STRING)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@CardTypeID", CardTypeID)
            };

            return SqlDBHelper.ExecuteNonQuery("DeleteCardType", CommandType.StoredProcedure, CONNECTION_STRING, parameters);
        }

        /// <summary>
        /// Get CardType Details
        /// </summary>
        /// <param name="CardTypeID"></param>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public CardType GetCardTypeDetails(String CardTypeID, String CONNECTION_STRING)
        {
            CardType objCardType = null;

            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@CardTypeID",CardTypeID),
            };
            //Lets get the list of all CardType in a datataable
            using (DataTable table = SqlDBHelper.ExecuteParamerizedSelectCommand("GetCardTypeDetails", CommandType.StoredProcedure, CONNECTION_STRING, parameters))
            {
                //check if any record exist or not
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    //Lets go ahead and create the list of CardType
                    objCardType = new CardType();

                    //Now lets populate CardType details into the list of CardType                                         
                    objCardType.CardTypeID = Convert.ToInt32(row["CardTypeID"]);
                    objCardType.Created = row["Created"].ToString();
                    objCardType.Updated = row["Updated"].ToString();
                    objCardType.Isactive = row["Isactive"].ToString();
                    objCardType.Name = row["Name"].ToString();
                    objCardType.Descriptions = row["Descriptions"].ToString();
                }
            }
            return objCardType;
        }

        /// <summary>
        /// Get CardType List
        /// </summary>
        /// <param name="CONNECTION_STRING"></param>
        /// <returns></returns>
        public List<CardType> GetCardTypeList(String CONNECTION_STRING)
        {
            List<CardType> listCardType = null;

            //Lets get the list of all CardType in a datataable
            using (DataTable table = SqlDBHelper.ExecuteSelectCommand("GetCardTypeList", CommandType.StoredProcedure, CONNECTION_STRING))
            {
                //check if any record exist or not
                if (table.Rows.Count > 0)
                {
                    //Lets go ahead and create the list of CardType
                    listCardType = new List<CardType>();

                    //Now lets populate CardType details into the list of CardType
                    foreach (DataRow row in table.Rows)
                    {
                        CardType objCardType = new CardType();
                        objCardType.CardTypeID = Convert.ToInt32(row["CardTypeID"]);
                        objCardType.Created = row["Created"].ToString();
                        objCardType.Updated = row["Updated"].ToString();
                        objCardType.Isactive = row["Isactive"].ToString();
                        objCardType.Name = row["Name"].ToString();
                        objCardType.Descriptions = row["Descriptions"].ToString();

                        listCardType.Add(objCardType);
                    }
                }
            }

            return listCardType;
        }        

    }
}
